package com.hunray.rent.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

@Entity
@Table(name = "t_role")
public class Role implements Serializable {

	private static final long serialVersionUID = 1L;

	private Integer id;
	
	private String no;

	private String name; // 中文名称

	private String desrc;

	private String identityName; // 唯一名称，用于程序中进行判断，例如：admin
	
	private Department department;

	private List<RolePermission> rolePermissions = new ArrayList<RolePermission>();

	private List<UserRole> userRoles = new ArrayList<UserRole>();

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "tg_role")
	@TableGenerator(name = "tg_role", table = "t_id", pkColumnName = "f_table", pkColumnValue = "t_role", valueColumnName = "f_id", initialValue = 1, allocationSize = 1)
	@Column(name = "f_id")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name="f_no",nullable=false,unique=true)
	@NotNull
	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}
	
	@Column(name = "f_name", nullable = false, unique = true)
	@NotNull
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "f_descr", length = 1024)
	@Length(max = 1024)
	public String getDesrc() {
		return desrc;
	}

	public void setDesrc(String desrc) {
		this.desrc = desrc;
	}

	@Column(name = "f_identity_name", nullable = false, unique = true)
	@NotNull
	public String getIdentityName() {
		return identityName;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="f_department")
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public void setIdentityName(String nickname) {
		this.identityName = nickname;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "role")
	public List<RolePermission> getRolePermissions() {
		return rolePermissions;
	}

	public void setRolePermissions(List<RolePermission> rolePermissions) {
		this.rolePermissions = rolePermissions;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "role")
	public List<UserRole> getUserRoles() {
		return userRoles;
	}

	public void setUserRoles(List<UserRole> userRoles) {
		this.userRoles = userRoles;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	
	@Override
	public String toString() {
		return "Role [name=" + name + ", identityName=" + identityName + "]";
	}

	@Transient
	public List<User> getUsers() {
		List<User> users = new ArrayList<User>();
		for (UserRole userRole : getUserRoles()) {
			users.add(userRole.getUser());
		}
		return users;
	}

	@Transient
	public List<Permission> getPermissions() {
		List<Permission> permissions = new ArrayList<Permission>();
		for (RolePermission rolePermission : getRolePermissions()) {
			permissions.add(rolePermission.getPermission());
		}
		return permissions;
	}
	@Transient
	public List<String> getPermissionNames() {
		List<String> permissionNames = new ArrayList<String>();
		for (Permission permission : getPermissions()) {
			permissionNames.add(permission.getName());
		}
		return permissionNames;
	}
}
